/**
 * 
 */
package com.flashdeck.dao.impl;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.content.Context;

import com.flashdeck.dao.DeckDao;
import com.flashdeck.dao.SettingsDao;
import com.flashdeck.domain.Deck;
import com.flashdeck.domain.DeckInfo;

/**
 * @author Vincentas
 * 
 */
public class DeckDaoImpl implements DeckDao {

	private final SettingsDao settingsDao;

	private Context context;

	public DeckDaoImpl(Context context) {
		this.context = context;
		settingsDao = new SettingsDaoImpl(context);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.flashdeck.dao.DeckDao#listDeck()
	 */
	@Override
	public List<DeckInfo> listDeckInfos() {
		File[] deckFiles = getDeckFiles();

		Set<DeckInfo> result = new HashSet<DeckInfo>();
		for (File deckFile : deckFiles) {
			DeckInfo deckInfo = Deck.parseDeckInfo(deckFile);
			if (deckInfo != null) {
				result.add(deckInfo);
			}
		}

		return new ArrayList<DeckInfo>(result);
	}

	@Override
	public File[] getDeckFiles() {
		return getSettingsDao().getDeckFolder().listFiles(new FilenameFilter() {

			private static final String DECK_FILE_EXTENCION = "deck";

			@Override
			public boolean accept(File dir, String filename) {
				return filename.indexOf(DECK_FILE_EXTENCION) == filename.length() - DECK_FILE_EXTENCION.length();
			}
		});
	}

	/**
	 * @return the settingsDao
	 */
	public SettingsDao getSettingsDao() {
		return settingsDao;
	}

	@Override
	public void removeDeck(DeckInfo deckInfo) {
		// TODO Auto-generated method stub		
	}

}
